/**
 * 获取所有组织节点
 * @author houxy
 */
function findFather() {
    $.ajax({
        url: "getNodeList",
        type: "get",
        dataType: "json",
        cache: "false",
        success: function (data) {
            let setting2 = {
                data: {
                    simpleData: {
                        enable: true,
                        rootPId: 0
                    }
                },
                callback: {
                    onClick: onClick
                },
                view: {
                    showIcon: false
                }
            };
            $.fn.zTree.init($("#treeDemo"), setting2, data);
        },
        error: function () {
            alert("系统繁忙，请稍后重试！");
        }
    })
}

/**
 * click回调函数
 * @author houxy
 * @param event
 * @param treeId
 * @param treeNode
 */
function onClick(event, treeId, treeNode) {
    let nodeFid = treeNode.pId;
    //alert(nodeFid);
    let nodeId = treeNode.id;
    let nodeName = treeNode.name;
    $("#txt_Forlderid").val(nodeId);
    showButton(nodeFid, nodeId, nodeName);
}

/**
 * 通过节点信息显示对应的按钮
 * @param nodeFid
 * @param nodeId
 * @param nodeName
 * @author houxy
 */
function showButton(nodeFid, nodeId, nodeName) {
    if (nodeFid == 0 || nodeFid == null) {
        $("#delete").hide();
        $("#add").show();
        $("#alter").hide();
        $("#name").hide();
        $("#move").hide();
    } else {
        $("#delete").show();
        $("#add").show();
        $("#alter").show();
        $("#name").show();
        $("#input_name").val(nodeName);
        $("#move").show();
        $("#can").hide();
    }
    $("#input_id").val(nodeId);
    $("#newName").hide();
    $("#new_name").val("");
}

/**
 * 修改节点名称
 * @author houxy
 */
function alterName() {
    let name = $("#input_name").val().trim();
    if (name == '') {
        alert("节点名不能为空！");
        return;
    }
    let nodeId = $("#input_id").val();
    $.ajax({
        url: "alterName",
        type: "post",
        data: {"deptName": name, "id": nodeId},
        dataType: "json",
        success: function (data) {
            let status = data.status
            if(status == 0){
                alert(data.msg, ctx + "/toTreeNode");
            }else{
                alert("修改失败！");
            }
        },
        error: function () {
            alert("修改失败！");
        }
    })
}

/**
 * 新建节点
 * @author houxy
 */
function newNode() {
    let nodeId = $("#input_id").val();
    //alert(nodeId);
    let check = true;
    let newName = $("#new_name").val().trim();
    if (newName == null || newName == "") {
        alert("节点名不能为空！");
        check = false;
    }
    if (nodeId == null || nodeId == "") {
        alert("请选择一个父节点！");
        check = false;
    }
    if (check) {
        $.ajax({
            url: "newNode",
            data: {"pid": nodeId, "deptName": newName},
            cache: "false",
            type: "post",
            dataType: "json",
            success: function (data) {
                let status = data.status
                if(status == 0){
                    alert(data.msg, ctx + "/toTreeNode");
                }else{
                    alert("添加失败！");
                }
            },
            error: function () {
                alert("添加失败！");
            }
        })
    }
}

/**
 * 删除节点
 * @author houxy
 */
function deleteNode() {
    let nodeId = $("#input_id").val();
    let zTree = $.fn.zTree.getZTreeObj("treeDemo");
    let node = zTree.getNodeByParam("id", nodeId, null);
    if (node.isParent) {
        confirm("注意:删除此节点会将其所有子节点删除，确认删除吗？", function (result) {
            if (result) {
                sureDeleteNode(nodeId);
            }
        });

    } else {
        confirm("确定要删除此节点吗？", function (result) {
            if (result) {
                sureDeleteNode(nodeId);
            }
        });
    }
}

//确认删除
function sureDeleteNode(nodeId) {
    $.ajax({
        url: "deleteNode",
        type: "post",
        data: {"nodeId": nodeId},
        dataType: "json",
        success: function (data) {
            let status = data.status
            if(status == 0){
                alert(data.msg, ctx + "/toTreeNode");
            }else{
                alert("删除失败，稍后重试！");
            }
        },
        error: function () {
            alert("删除失败，稍后重试！");
        }
    })
}

/**
 * 显示转移页面
 * @author houxy
 */
function alertMove() {
    //获取全部节点
    getTreeDate1();
    //清空模态框数据
    document.getElementById('input_contentForlderName_showName').innerHTML = "";
    $("#nodeId").val("")
    $("#nodeFId").val("");
    let nodeId = $("#input_id").val();
    let nodeName = $("#input_name").val();
    if (nodeId == "" || nodeName == "") {
        alert("请选择一个节点！");
        return;
    }
}

/**
 * 确认转移逻辑处理
 * @author houxy
 */
function OKAction() {
    //想转移的节点
    let sid = $("#input_id").val();
    //转移到哪个节点下
    let pid = $("#nodeId").val();
    //转移到哪个节点的父节点
    let nodeFid = $("#nodeFId").val();
    //如果没有选择转移到哪个节点
    if (pid == "" || pid == null) {
        alert("请选择一个节点！");
        return;
    }
    //如果想转移的节点和选择的节点一样
    if (sid == pid) {
        alert("请选择另外一个节点！");
        return;
    }
    //如果想转移的节点和选择节点的父亲是同一个节点
    if (nodeFid == sid) {
        alert("拒绝转移！");
        return;
    }
    $.ajax({
        url: "moveNode",
        type: "post",
        data: {"sid": sid, "pid": pid},
        dataType: "json",
        success: function (data) {
            let status = data.status
            if(status == 0){
                alert(data.msg, ctx + "/toTreeNode");
            }else{
                alert("转移失败！");
            }
        },
        error: function () {
            alert("转移失败！");
        }
    })
}

/**
 * 取消逻辑处理
 * @author houxy
 */
function cancelAction() {
    $("#new_name").val("");
}

/**
 * getTreeDate1的回调函数
 * @author houxy
 */
function onClick1(event, treeId, treeNode, clickFlag) {
    document.getElementById('input_contentForlderName_showName').innerHTML = treeNode.name;
    $("#nodeId").val(treeNode.id);
    $("#nodeFId").val(treeNode.pId);
}

/**
 * 转移页面获取所有组织节点
 * @author houxy
 */
function getTreeDate1() {
    $.ajax({
        type: "get",
        url: "getNodeList",
        dataType: "json",
        success: function (data) {
            let setting1 = {
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    onClick: onClick1
                },
                view: {
                    showIcon: false
                }
            };
            $.fn.zTree.init($("#treeDemo1"), setting1, data);
        }
    });
}